package com.wpw.java8demo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.stream.Collectors;

import org.junit.Before;
import org.junit.Test;

public class StreamTest {
	List<Apple> appList;

	@Before
	public void before() {
		appList = new ArrayList<>();
		Apple app;
		for (int i = 0; i < 20; i++) {
			app = new Apple();
			app.setColor(String.valueOf(i));
			app.setWeigth(new Random().nextInt(10));
			appList.add(app);
		}
	}

	@Test
	public void test1() {
		appList.stream().filter(a -> a.getWeigth() < 4).map(Apple::getColor).limit(2).collect(Collectors.toList())
				.forEach(System.out::println);
	}

	@Test
	public void test2() {

		List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 1, 3, 5, 4, 8, 8, 10);
		list.stream().filter(i -> i % 2 == 0).distinct().sorted(Comparator.reverseOrder()).forEach(System.out::println);
	}

	// flatMap是把一个流中的每个值都换成另一个流，然后把所有的流连接成一个流
	@Test
	public void test3() {
		String[] arrays = { "hello", "world" };
		Arrays.stream(arrays).map(word -> word.split(""))
				.flatMap(Arrays::stream)
				.distinct()
				.collect(Collectors.toList())
				.forEach(System.out::println);
	}

	@Test
	public void test31() {
		String[] arrays = { "hello", "world" };
		Arrays.stream(arrays).map(word -> word.split(""))
				.map(Arrays::stream)
				.distinct()
				.collect(Collectors.toList())
				.forEach(System.out::println);
	}

	@Test
	public void test32() {

		List<User> users = new ArrayList<>();
		users.add(new User(Arrays.asList("1","2","3")));
		users.add(new User(Arrays.asList("4","5","6")));


		List<String> result = users.stream()
				.map(user -> user.getBody())
				.flatMap(Collection::stream)
				.distinct()
				.collect(Collectors.toList());
		System.out.println(result);
	}

	@Test
	public void test4() {
		String[] arrays = { "hello", "world2" };
		System.out.println(Arrays.stream(arrays).anyMatch(s -> s.length() == 5));

		System.out.println(Arrays.stream(arrays).allMatch(s -> s.length() == 5));

		System.out.println(Arrays.stream(arrays).noneMatch(s -> s.length() == 5));

	}

	@Test
	public void test5() {
		String[] arrays = { "hello", "world2" };
		Optional<String> findAny = Arrays.stream(arrays).filter(s -> s.length() == 7).findAny();
		System.out.println(findAny.orElseGet(() -> "dd"));
	}

	@Test
	public void test6() {
		Optional<Apple> findFirst = appList.stream().limit(2).skip(2).findFirst();
		System.out.println(findFirst.orElseGet(() -> new Apple()));
	}

	@Test
	public void test7() {
		List<Integer> list = new ArrayList<>();
		list.add(1);list.add(1);list.add(2);list.add(3);
		List<Integer> collect = list.stream().distinct().collect(Collectors.toList());
		System.out.println(collect);
	}

	/**
	 * 单个数据执行完一个操作，再执行下一个操作，执行完后，下一个数据再继续按照这个流程执行
	 */
	@Test
	public void executeOrder() {
		List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
		List<Integer> collect = list.stream().filter(i -> {
			System.out.println("filter===" + i);
			return i < 6;
		}).map(i -> {
			System.out.println("map===" + i);
			return i;
		}).peek(i -> {
			System.out.println("peek===" + i);
		}).collect(Collectors.toList());

	}

}

class User {
	private List<String> body;

	public User(List<String> body) {
		this.body = body;
	}

	public List<String> getBody() {
		return body;
	}

	public void setBody(List<String> body) {
		this.body = body;
	}

}
